﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using BLL;
using Utility;
using System.IO;

public partial class VehicleUI_Employee_EmployeeAMV : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.QueryString["Type"] == null)
            { }
            else if (Request.QueryString["Type"].ToString() == "View")
            {
                LoadService();
                txt_JoinDate.Enabled = false;
                txt_EmployeeID.Enabled = false;
                txt_Name.Enabled = false;
                txt_Nation.Enabled = false;
                txt_Native.Enabled = false;
                txt_PersonID.Enabled = false;
                txt_Position.Enabled = false;
                txt_Tel.Enabled = false;
                txt_Mobile.Enabled = false;
                txt_DriverNumber.Enabled = false;
                txt_BirthDay.Enabled = false;
                txt_Address.Enabled = false;
                ddl_Education.Enabled = false;
                rb_Nan.Enabled = false;
                rb_Nv.Enabled = false;
                chk_Driver.Enabled = false;


                ful_CHK.Visible = false;
                btn_Sure.Visible = false;
            }
            else if (Request.QueryString["Type"].ToString() == "Edit")
            {
                LoadService();
            }
        }
    }

    protected void LoadService()
    {
        string _id = Request.QueryString["ID"].ToString();
        Employee cs = new EmployeeBLL().GetModel(_id);
        if (cs != null)
        {
            txt_EmployeeID.Text = cs.EmployeeID;
            txt_JoinDate.Text = cs.JoinDate.ToString();
            txt_Name.Text = cs.Name.ToString();
            txt_Nation.Text = cs.Nation;
            txt_Native.Text = cs.Native;
            txt_PersonID.Text = cs.PersonID;
            txt_Position.Text = cs.position;
            txt_Tel.Text = cs.Telephone;
            txt_Mobile.Text = cs.Mobile;
            txt_DriverNumber.Text = cs.DriveLicenseNO;
            txt_BirthDay.Text = cs.LeaveDate.ToString();
            txt_Address.Text = cs.Address;
            ddl_Education.SelectedValue = cs.Education;
            rb_Nan.Checked = cs.Sex;
            chk_Driver.Checked = cs.IsDriver;
            if (cs.Photo != string.Empty)
            {
                img_CHK.ImageUrl = GlobalArgs.EmployeePath + cs.Photo;
            }
        }
    }

    protected void btn_Sure_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["Type"] == null)
        {
            Employee cs = new Employee();

            cs.EmployeeID = txt_EmployeeID.Text.Trim();
            cs.JoinDate = DateTime.Parse(txt_JoinDate.Text.Trim());
            cs.Name = txt_Name.Text.Trim();
            cs.Native = txt_Native.Text.Trim();
            cs.Nation = txt_Nation.Text.Trim();
            cs.PersonID = txt_PersonID.Text.Trim();
            cs.position = txt_Position.Text.Trim();
            cs.Telephone = txt_Tel.Text.Trim();
            cs.Mobile = txt_Mobile.Text.Trim();
            cs.DriveLicenseNO = txt_DriverNumber.Text.Trim();
            cs.LeaveDate = DateTime.Parse(txt_BirthDay.Text.Trim());
            cs.Address = txt_Address.Text.Trim();
            cs.Education = ddl_Education.SelectedValue.ToString();
            cs.Sex = rb_Nan.Checked;
            cs.IsDriver = chk_Driver.Checked;
            cs.OrganID = (int)Session["OrganID"];
            cs.InputBy = Session["UserID"].ToString();

            string f2 = UploadFile(ful_CHK, 1);

            cs.Photo = f2.Length > 5 ? f2 : "";

            bool re = new EmployeeBLL().Add(cs);

            if (re)
            {
                string msg = "";

                if (f2 == "err")
                {
                    msg += "头像上传失败!";
                }
                UtilityService.AlertAndRedirect(this.Page, "添加成功! " + msg, "EmployeeMgr.aspx");
            }
            else
            {
                FileInfo file2 = new FileInfo(Server.MapPath(GlobalArgs.EmployeePath) + f2);
                if (file2.Exists)
                {
                    file2.Delete();
                }
                UtilityService.Alert(this.Page, "添加失败! ");
            }
        }
        else if (Request.QueryString["Type"] == "Edit")
        {
            string _id = Request.QueryString["ID"].ToString();
            Employee cs = new EmployeeBLL().GetModel(_id);
            cs.EmployeeID = txt_EmployeeID.Text.Trim();
            cs.JoinDate = DateTime.Parse(txt_JoinDate.Text.Trim());
            cs.Name = txt_Name.Text.Trim();
            cs.Nation = txt_Nation.Text.Trim();
            cs.PersonID = txt_PersonID.Text.Trim();
            cs.position = txt_Position.Text.Trim();
            cs.Telephone = txt_Tel.Text.Trim();
            cs.Mobile = txt_Mobile.Text.Trim();
            cs.DriveLicenseNO = txt_DriverNumber.Text.Trim();
            cs.LeaveDate = DateTime.Parse(txt_BirthDay.Text.Trim());
            cs.Address = txt_Address.Text.Trim();
            cs.Education = ddl_Education.SelectedValue.ToString();
            cs.Sex = rb_Nan.Checked;
            cs.IsDriver = chk_Driver.Checked;


            string f2 = "";
            if (ful_CHK.HasFile)
            {
                f2 = UploadFile(ful_CHK, 1);
                cs.Photo = f2.Length > 5 ? f2 : "";
            }

            bool re = new EmployeeBLL().Update(cs);

            if (re)
            {
                string msg = "";

                if (f2 == "err")
                {
                    msg += "检查图片上传失败!";
                }
                UtilityService.AlertAndRedirect(this.Page, "修改成功! " + msg, "EmployeeMgr.aspx");
            }
            else
            {

                FileInfo file2 = new FileInfo(Server.MapPath(GlobalArgs.EmployeePath) + f2);
                if (file2.Exists)
                {
                    file2.Delete();
                }

                UtilityService.Alert(this.Page, "修改失败! ");
            }
        }
    }

    public string UploadFile(FileUpload ful, int type)
    {

        try
        {

            if (!ful.HasFile)
            {
                // this.Upload_info.Text = "请选择上传文件！";
                return "none";
            }
            else
            {
                string filepath = ful.PostedFile.FileName;  //得到的是文件的完整路径,包括文件名，如：C:\My Pictures\20022775_m.jpg 
                string filename = "";
                string ext = filepath.Substring(filepath.LastIndexOf("."));
                filename = Session["OrganID"].ToString() + "_" + DateTime.Now.Ticks.ToString() + "_head" + ext;//取得文件在服务器上保存的位置C:\Inetpub\20022775_m.jpg
                //if (type == 0)
                //{

                //    filename = Session["OrganID"].ToString() + "_" + DateTime.Now.Ticks.ToString() + "_gps" + ext;//取得文件在服务器上保存的位置C:\Inetpub\20022775_m.jpg
                //}
                //else
                //{
                //    filename = Session["OrganID"].ToString() + "_" + DateTime.Now.Ticks.ToString() + "_chk" + ext;
                //}

                string serverpath = Server.MapPath(GlobalArgs.EmployeePath) + filename;//取得文件在服务器上保存的位置C:\Inetpub\wwwroot\WebSite1\images\20022775_m.jpg

                ful.PostedFile.SaveAs(serverpath);//将上传的文件另存为 
                //this.Upload_info.Text = "上传成功！";
                return filename;
            }
        }
        catch (Exception ex)
        {
            UtilityService.Alert(this.Page, "上传发生错误!原因是:" + ex.ToString());
            //this.Upload_info.Text = "上传发生错误！原因是：" + ex.ToString();
            return "err";
        }

    }


    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        UtilityService.Open(this.Page, "../../ImageView.aspx?url=" + ((ImageButton)sender).ImageUrl);
    }
}
